<template>
  <el-drawer v-model="trainDialog" :before-close="showBtn" class="ivr-train-drawer">
    <template #header>
      <div class="drawer-header">
        <div class="header-icon">
          <Icon icon="ep:cpu" />
        </div>
        <div class="header-content">
          <h3 class="drawer-title">IVR训练</h3>
          <p class="drawer-subtitle">智能对话训练与测试</p>
        </div>
      </div>
    </template>

    <template #default>
      <div class="chat-container" ref="scrollbarBox">
        <div height="100%" class="chat-scrollbar">
          <div class="chat-content">
            <div
              class="message-wrapper"
              :class="item.khtype == 1 ? 'user-message' : 'bot-message'"
              v-for="item in chat_List"
              :key="item.id"
            >
              <div class="message-header">
                <div class="avatar" :class="item.khtype == 0 ? 'bot-avatar' : 'user-avatar'">
                  <Icon :icon="item.khtype == 0 ? 'ep:robot' : 'ep:user'" />
                </div>
                <span class="sender-name">
                  {{ item.khtype == 0 ? '节点: ' + item.nodeName : '用户' }}
                </span>
              </div>

              <div class="message-bubble" :class="item.khtype == 0 ? 'bot-bubble' : 'user-bubble'">
                <div class="message-text">{{ item.khcontent }}</div>
              </div>
            </div>

            <!-- 空状态 -->
            <div v-if="chat_List.length === 0" class="empty-state">
              <Icon icon="ep:chat-dot-round" class="empty-icon" />
              <p class="empty-text">开始对话训练</p>
              <p class="empty-desc">输入消息开始与IVR系统进行对话训练</p>
            </div>
          </div>
        </div>
      </div>
    </template>

    <template #footer>
      <div class="chat-footer">
        <div class="input-area">
          <el-input
            v-model.trim="txt"
            clearable
            @keydown.enter="send"
            placeholder="请输入内容"
            :disabled="loading"
            class="chat-input"
          >
            <template #suffix>
              <el-button
                type="primary"
                @click="send"
                :disabled="loading"
                :loading="loading"
                class="send-btn"
                text
              >
                <Icon icon="ep:promotion" />
              </el-button>
            </template>
          </el-input>
        </div>

        <div class="action-buttons">
          <el-button :disabled="loading" @click="rest" class="reset-btn">
            <Icon icon="ep:refresh" class="btn-icon" />
            重置会话
          </el-button>
        </div>
      </div>
    </template>
  </el-drawer>
</template>

<script setup>
import { irvFlowTraining } from '@/api/call/ivr/index.ts'
import { generateUUID } from '@/utils'

defineOptions({ name: 'IvrTrain' })

const trainDialog = ref(false)
const uuid = ref(null)
const txt = ref('')
const chat_List = ref([])
const loading = ref(false)
const scrollbarBox = ref(null)

const props = defineProps({
  ivrId: String
})

const emit = defineEmits(['setShow'])

const open = () => {
  if (!uuid.value) uuid.value = generateUUID()
  trainDialog.value = true
  if (chat_List.value.length == 0) {
    send()
  }
}

const rest = () => {
  uuid.value = generateUUID()
  chat_List.value = []
  send()
}

const showBtn = () => {
  trainDialog.value = false
  emit('setShow')
}

// 滚动到底部的方法
const scrollToBottom = () => {
  nextTick(() => {
    if (scrollbarBox.value) {
      scrollbarBox.value.scrollTop = scrollbarBox.value.scrollHeight
    }
  })
}

const send = async () => {
  loading.value = true
  try {
    const obj = {
      trainingId: uuid.value,
      message: txt.value,
      flowId: props.ivrId
    }
    const data = await irvFlowTraining(obj)
    chat_List.value = data.chat_List
    txt.value = ''

    // 滚动到底部
    setTimeout(() => {
      scrollToBottom()
    }, 1e2)

    loading.value = false
  } catch {
    loading.value = false
  }
}

defineExpose({ open })
</script>

<style lang="scss" scoped>
.ivr-train-drawer {
  :deep(.el-drawer) {
    border-radius: 16px 0 0 16px;
  }

  :deep(.el-drawer__header) {
    margin-bottom: 0;
    padding: 0;
    border-bottom: 1px solid rgba(0, 82, 204, 0.1);
  }

  :deep(.el-drawer__body) {
    padding: 0;
    display: flex;
    flex-direction: column;
  }

  :deep(.el-drawer__footer) {
    padding: 0;
    margin-top: 0;
  }
}

/* 头部样式 */
.drawer-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 24px;
  background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  color: white;
}

.header-icon {
  width: 40px;
  height: 40px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  backdrop-filter: blur(10px);
}

.header-content {
  flex: 1;
}

.drawer-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 4px 0;
}

.drawer-subtitle {
  font-size: 14px;
  margin: 0;
  opacity: 0.9;
}

/* 聊天容器 */
.chat-container {
  height: 100%;
  overflow-y: auto;
  background: #f8fafc;
  scroll-behavior: smooth;
}

.chat-scrollbar {
  height: 100%;
}

.chat-content {
  padding: 20px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  overflow-y: auto;
}

/* 消息样式 */
.message-wrapper {
  display: flex;
  flex-direction: column;
  gap: 8px;

  &.user-message {
    align-items: flex-end;
  }

  &.bot-message {
    align-items: flex-start;
  }
}

.message-header {
  display: flex;
  align-items: center;
  gap: 8px;

  .user-message & {
    flex-direction: row-reverse;
  }
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: white;

  &.bot-avatar {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%);
  }

  &.user-avatar {
    background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
  }
}

.sender-name {
  font-size: 12px;
  color: #6b7280;
  font-weight: 500;
}

.message-bubble {
  max-width: 80%;
  padding: 12px 16px;
  border-radius: 16px;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);

  &.bot-bubble {
    background: white;
    border: 1px solid rgba(0, 82, 204, 0.1);

    &::before {
      content: '';
      position: absolute;
      top: -8px;
      left: 16px;
      width: 0;
      height: 0;
      border-left: 8px solid transparent;
      border-right: 8px solid transparent;
      border-bottom: 8px solid white;
    }
  }

  &.user-bubble {
    background: linear-gradient(135deg, #0052cc 0%, #1d4ed8 100%);
    color: white;

    &::before {
      content: '';
      position: absolute;
      top: -8px;
      right: 16px;
      width: 0;
      height: 0;
      border-left: 8px solid transparent;
      border-right: 8px solid transparent;
      border-bottom: 8px solid #0052cc;
    }
  }
}

.message-text {
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
}

/* 空状态 */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  text-align: center;
  padding: 40px 20px;
}

.empty-icon {
  font-size: 64px;
  color: #d1d5db;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 18px;
  font-weight: 600;
  color: #374151;
  margin: 0 0 8px 0;
}

.empty-desc {
  font-size: 14px;
  color: #6b7280;
  margin: 0;
}

/* 底部输入区域 */
.chat-footer {
  background: white;
  border-top: 1px solid rgba(0, 82, 204, 0.1);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.input-area {
  display: flex;
  gap: 12px;
}

.chat-input {
  flex: 1;

  :deep(.el-input__wrapper) {
    border-radius: 24px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
    transition: all 0.2s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      box-shadow: 0 4px 12px rgba(0, 82, 204, 0.2);
    }
  }

  :deep(.el-input__inner) {
    padding-right: 50px;
  }
}

.send-btn {
  color: #0052cc;
  padding: 8px;
  border-radius: 50%;

  &:hover {
    background: rgba(0, 82, 204, 0.1);
  }

  &:disabled {
    color: #d1d5db;
  }
}

.action-buttons {
  display: flex;
  justify-content: center;
}

.reset-btn {
  border: 2px solid #e5e7eb;
  color: #6b7280;
  border-radius: 8px;

  &:hover {
    border-color: #0052cc;
    color: #0052cc;
  }

  .btn-icon {
    margin-right: 6px;
  }
}

/* 响应式设计 - H5兼容 */
@media (max-width: 768px) {
  .ivr-train-drawer {
    :deep(.el-drawer) {
      width: 100% !important;
      border-radius: 0;
    }
  }

  .drawer-header {
    padding: 20px;
  }

  .chat-content {
    padding: 16px;
  }

  .message-bubble {
    max-width: 90%;
  }

  .chat-footer {
    padding: 16px;
  }

  .empty-state {
    padding: 30px 16px;
  }

  .empty-icon {
    font-size: 48px;
  }
}

@media (max-width: 480px) {
  .drawer-header {
    padding: 16px;
    flex-direction: column;
    text-align: center;
    gap: 12px;
  }

  .chat-content {
    padding: 12px;
    gap: 12px;
  }

  .message-bubble {
    max-width: 95%;
    padding: 10px 12px;
  }

  .chat-footer {
    padding: 12px;
  }

  .action-buttons {
    margin-top: 8px;
  }

  .reset-btn {
    width: 100%;
  }
}

/* 深色模式适配 */
@media (prefers-color-scheme: dark) {
  .chat-container {
    background: #1f2937;
  }

  .message-bubble.bot-bubble {
    background: #374151;
    border-color: rgba(255, 255, 255, 0.1);

    &::before {
      border-bottom-color: #374151;
    }
  }

  .message-text {
    color: #e5e7eb;
  }

  .chat-footer {
    background: #374151;
    border-top-color: rgba(255, 255, 255, 0.1);
  }

  .empty-text {
    color: #f9fafb;
  }

  .empty-desc {
    color: #d1d5db;
  }
}
</style>
